package com.ruoyi.project.platform.mapper;

import java.util.List;

import com.ruoyi.project.platform.domain.RRobotRunning;
import com.ruoyi.project.platform.dto.UserRunningDto;
import org.apache.ibatis.annotations.*;

/**
 * 机器人实例Mapper接口
 *
 * @author ruoyi
 * @date 2020-08-27
 */
@Mapper
public interface RRobotRunningMapper {
    /**
     * 查询机器人实例
     *
     * @param runningId 机器人实例ID
     * @return 机器人实例
     */
    public RRobotRunning selectRRobotRunningById(Long runningId);

    /**
     * 查询机器人实例列表
     *
     * @param rRobotRunning 机器人实例
     * @return 机器人实例集合
     */
    public List<RRobotRunning> selectRRobotRunningList(RRobotRunning rRobotRunning);

    /**
     * 新增机器人实例
     *
     * @param rRobotRunning 机器人实例
     * @return 结果
     */
    public int insertRRobotRunning(RRobotRunning rRobotRunning);

    /**
     * 修改机器人实例
     *
     * @param rRobotRunning 机器人实例
     * @return 结果
     */
    public int updateRRobotRunning(RRobotRunning rRobotRunning);

    @Update("UPDATE `r_robot_running`  " +
            "SET container_id = '', " +
            "state = ''  " +
            "WHERE " +
            " running_id =#{runningId}" +
            "")
    public int deleteRobootStatus(@Param("runningId") Long runningId);

    /**
     * 删除机器人实例
     *
     * @param runningId 机器人实例ID
     * @return 结果
     */
    public int deleteRRobotRunningById(Long runningId);

    /**
     * 批量删除机器人实例
     *
     * @param runningIds 需要删除的数据ID
     * @return 结果
     */
    public int deleteRRobotRunningByIds(Long[] runningIds);

    @Select("SELECT " +
            "e.platform, " +
            "e.tag_name AS tagName, " +
            "e.sequence, " +
            "api.secret_key AS secretKey, " +
            "api.passphrase, " +
            "api.api_key AS apiKey, " +
            "api.description, " +
            "symbol.tick_size AS tickSize, " +
            "symbol.size_increment AS sizeIncrement, " +
            "symbol.quote_currency AS quoteCurrency, " +
            "symbol.base_currency AS baseCurrency, " +
            "symbol.min_size AS minSize, " +
            "symbol.`name`  " +
            "FROM " +
            " r_running_exchange e " +
            " JOIN r_robot_running r ON e.running_id = r.running_id " +
            " JOIN plt_user_api api ON e.api_id = api.api_id " +
            " JOIN plt_user_symbol symbol ON e.symbol_id = symbol.symbol_id" +
            " AND  r.container_id =#{containerId} ")
    public List<UserRunningDto.ExchangeDto> exchanges(@Param("containerId") String containerId);

    @Select("SELECT " +
            " m.parameter_value AS `value`, " +
            " p.description, " +
            " p.`name`  " +
            "FROM " +
            " r_running_mapping m " +
            " LEFT JOIN r_robot_running r ON m.running_id = r.running_id " +
            " LEFT JOIN sty_parameter p ON m.parameter_id = p.parameter_id  " +
            "WHERE " +
            " p.`name` IS NOT NULL" +
            " AND  r.container_id =#{containerId} ")
    public List<UserRunningDto.MappingDto> mappings(@Param("containerId") String containerId);

    @Select("SELECT " +
            " running_id AS runningId, " +
            " NAME AS NAME, " +
            " strategy_id AS strategyId, " +
            " strategy_name AS strategyName, " +
            " strategy_type AS strategyType, " +
            " robot_type AS robotType, " +
            " user_id AS userId, " +
            " user_name AS userName, " +
            " running_status AS runningStatus, " +
            " running_version AS runningVersion, " +
            " container_id AS containerId, " +
            " endpoint_id AS endpointId, " +
            " image_tag AS imageTag, " +
            " state AS state  " +
            "FROM " +
            " `r_robot_running`")
    public List<RRobotRunning> selectAll();

    @Select("<script> " +
            "SELECT " +
            " running_id AS runningId, " +
            " NAME AS NAME, " +
            " strategy_id AS strategyId, " +
            " strategy_name AS strategyName, " +
            " strategy_type AS strategyType, " +
            " robot_type AS robotType, " +
            " user_id AS userId, " +
            " user_name AS userName, " +
            " running_status AS runningStatus, " +
            " running_version AS runningVersion, " +
            " container_id AS containerId, " +
            " endpoint_id AS endpointId, " +
            " image_tag AS imageTag, " +
            " state AS state,  " +
            " platform  " +
            "FROM " +
            " `r_robot_running` where running_id in " +
            "<foreach item='item' index='index' collection='runningIds' open='(' separator=',' close=')'>" +
            " #{item}" +
            "</foreach>" +
            "</script>")
    public List<RRobotRunning> selectRRobotRunningListByIds(@Param("runningIds") Long[] runningIds);
}
